//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created Modif: 31/08/2005
// Last Modif: 31/08/2005

var XML_DDB={
  DOCDB:new ActiveXObject("Msxml2.DOMDocument"),
  DBALLDOCS:"",
  DOCDBFILE:"data/docsDB.xml",
  DEFDOCDBFILE:"defs/docsDB.xml"
}



function LoadDOCDB() {
  if(dos_loadxmlinit(XML_DDB.DOCDB,XML_DDB.DEFDOCDBFILE,XML_DDB.DOCDBFILE)!="error"){
    var root=XML_DDB.DOCDB.selectSingleNode("claimtool");
    if(!root) {
      alert("Malformed DOCDB file");
      return;
    }
    XML_DDB.DBALLDOCS=root.selectSingleNode("docs");
  }
}

LoadDOCDB();

function CreateDOCDB() {
  XML_DDB.DOCDB.load(XML_DDB.DEFDOCDBFILE); 
  XML_DDB.DOCDB.save(XML_DDB.DOCDBFILE);
}

function SaveDOCDB() {
  XML_DDB.DOCDB.save(XML_DDB.DOCDBFILE);
}

// TODO: normalize us apps 
function DocDBNormalize(pn) {
  var PN=trim(pn.toUpperCase());
  switch(PN.substring(0,2)) {
  case "XP":
    while(PN.length > 2 && PN.length < 11) { // "XP008022923".length;
      PN=PN.substring(0,2)+"0"+PN.substring(2,PN.length);
    }
    break;
  case "WO":
    var slash=PN.indexOf("/");
    if(slash>0) {
      PN=PN.substring(0,slash)+PN.substr(slash+1); // skip "/"
    }
    break;
  case "US":
    var slash=PN.indexOf("/");
    if(slash>0) {
      if(PN.substring(slash+1,slash+2)=="0") {
	PN=PN.substring(0,slash)+PN.substr(slash+2);// skip "/0"
      } else {
	PN=PN.substring(0,slash)+PN.substr(slash+1);// skip "/"
      }
    }
    break;
  }
  return PN; 
}

function is_DocXP(pn) {
  return pn.substr(0,2).toUpperCase()=="XP";
}


function CreateDocDB(pn,title,pd,pri) {
  var doc = XML_DDB.DOCDB.createElement("doc");
  var PN  = DocDBNormalize(pn);
  var attr;

  if(!pri) {
    pri=pd || "dd/mm/yyyy";
  }
  if(!pd) {
    pd=pri || "dd/mm/yyyy";
  }

  doc.setAttribute("pn",PN);
  doc.setAttribute("pd",pd);
  doc.setAttribute("fi",pri);
  
  if(!title) {
    if(is_DocXP(PN)) {
      title="XP documents need a title !";
    } else {
      title=PN;
    }
  }
  doc.setAttribute("title",title);
  
  doc.appendChild(XML_DDB.DOCDB.createElement("remarks"));
 
  return doc;
}


function FindDocDB(pn) {
  return XML_DDB.DBALLDOCS.selectSingleNode("doc [ @pn = '"+DocDBNormalize(pn)+"']");
}

// Add or update
function AddDocDB(pn,title,pd,pri) {
  var pn  = DocDBNormalize(pn);
  if(!FindDocDB(pn)) {
    XML_DDB.DBALLDOCS.appendChild(CreateDocDB(pn,title,pd,pri));
    XML_DDB.DBALLDOCS.appendChild(XML_DDB.DOCDB.createTextNode("\n"));
  } else {     
    SetDocDBTitle(pn,title);
    SetDocDBPD(pn,pd);
    SetDocDBPRI(pn,pri);
  }
  SaveDOCDB();
}


function SetDocDBAttr(pn,attr,val) {
  var node=FindDocDB(pn);
  return node && node.setAttribute(attr,val);
}

function GetDocDBAttr(pn,attr) {
  var node=FindDocDB(pn);
  return node && node.getAttribute(attr);
}


function SetDocDBPN(oldpn,newpn) {
  var node=FindDocDB(oldpn);
  if(node) {
    node.setAttribute("pn",newpn);
  }
}


function GetDocDBTitle(pn) {
  var node=FindDocDB(pn);
  if(node) {
    return node.getAttribute("title");
  } 
  return "not in database";
}

function GetDocDBPD(pn) {
  var node=FindDocDB(pn);
  if(node) {
    return node.getAttribute("pd");
  } 
  return "00/00/0000";
}

function GetDocDBPRI(pn) {
  var node=FindDocDB(pn);
  if(node) {
    return node.getAttribute("fi");
  } 
  return "00/00/0000";
}

function SetDocDBTitle(pn,title) {
  FindDocDB(pn).setAttribute("title",title);
}

function SetDocDBPD(pn,pd) {
  FindDocDB(pn).setAttribute("pd",pd);
}

function SetDocDBPRI(pn,fi) {
  FindDocDB(pn).setAttribute("fi",fi);
}



function GetDocDBRmks(pn,dossier,opt) {
  //alert("GetDocDBRmks("+pn+","+dossier+")");
  var text="";
  var _pn=DocDBNormalize(pn);  
  var _dossier=NormalizeDossierNumber(dossier);
  var node=FindDocDB(_pn); 
  if(node) {
    node=node.selectSingleNode("remarks");
  }
  if(node) {
    var rmk=node.selectSingleNode("rmk [ @dossier = '"+_dossier+"']");
    if(rmk) text=rmk.text;
  }
  return text;
}

function GetDocDBRmksDossiers(pn,dn) {
  var tab=new Array(dn);
  var node=FindDocDB(pn);
  if(node) {
    var trmks=node.selectNodes("remarks/rmk");
    //alert("found "+trmks.length+" dossiers citing "+pn);
    for(var i=0; i< trmks.length; i++) {
      var idn=trmks[i].getAttribute("dossier");
      if(idn!=dn) {
	tab.push(idn);
      }
    }
  }
  return tab;
}
  
  
function SetDocDBRmks(pn,dossier,rmks) {
  //alert("SetDocDBRmks("+pn+","+dossier+","+rmks+")");

  var _pn=DocDBNormalize(pn);
  var _dossier=NormalizeDossierNumber(dossier);
  var dbdoc=FindDocDB(_pn); 
  if(!dbdoc) {
    dbdoc=CreateDocDB(_pn);
  }
  var node=dbdoc.selectSingleNode("remarks");
  if(!node) {
       node = XML_DDB.DOCDB.createElement("remarks");
       dbdoc.appendChild(node);
  }	
  var rmk=node.selectSingleNode("rmk [ @dossier = '"+_dossier+"']");
  if(!rmk) {
      rmk=XML_DDB.DOCDB.createElement("rmk");
      node.appendChild(rmk);
  }
  rmk.setAttribute("dossier",_dossier);
  rmk.text=rmks; 	

} 

function GetDocDBReference(pn) {
  var txt="";
  var node=FindDocDB(pn);
  if(node) {
    if(is_DocXP(pn)) {
      return node.getAttribute("author")+" \""+node.getAttribute("title")+"\"";
    }
    return node.getAttribute("pn");
  }
}

function UpdateDocDB(pn,title,pd,pri) {
  var node=FindDocDB(pn);
  if(node) {
    node.setAttribute("title",title);
    node.setAttribute("pd",pd);
    node.setAttribute("fi",pri);
  }
}
